#[derive(Debug)]
enum SpreadsheetCell {
    Int(i32),
    Float(f64),
    Text(String),
}

fn main() {
    let mut v: Vec<i32> = vec![1, 2, 3, 4];
    println!("Hello, world!{:?}", v);
    let third: &i32 = &v[2];
    println!("The third element is {}", third);
    v.push(5);
    v.push(6);
    let i = 6;
    match v.get(i) {
        Some(third) => println!("The {} element is {}", i, third),
        None => println!("There is no {} element!", i),
    }

    if let Some(3) = v.get(2) {
        println!("We get third element is {}", 3);
    }

    for i in &mut v {
        *i += 50;
    }

    for i in v.iter().map(|x| println!("value: {}", &x)) {
        i
    }

    let result = (1..=5).fold(0, |acc, x| {
        println!("acc:{}", acc); //初始值
        println!("x:{}", x); //迭代值
        acc + x * x //结果作为下一下的初始值
    });
    println!("result = {}", result);

    let c = (1..4).chain(6..9);

    for i in c {
        print!("{} ", i);
    }
    // output: 1 2 3 6 7 8

    let r = (1..20).filter(|&x| x % 5 == 0).chain((6..9).rev());

    for i in r {
        print!("{} ", i);
    }
    // output: 5 10 15 8 7 6

    let cities = ["Toronto", "New York", "Melbourne"];
    let populations = [2_615_060, 8_550_405, 4_529_500];

    let matrix = cities.iter().zip(populations.iter());

    for (c, p) in matrix {
        println!("{:10}: population = {}", c, p);
    }

    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];
    for i in row {
        println!("{:?}", i);
    }
}
